package com.prj.ufdm.core.exception;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.prj.ufdm.core.dto.RetDataHead;
import com.prj.ufdm.core.enums.CoreRetCodeEnum;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;

/**
 * 公共异常处理方法
 * @author Administrator
 */
@ControllerAdvice
public class BaseExceptionHandler {
	
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ModelAndView Exception(HttpServletRequest req, Exception ex) {
        ModelAndView mv = new ModelAndView();
        FastJsonJsonView view = new FastJsonJsonView();
        RetDataHead retDataHead = new RetDataHead();
        retDataHead.setCode(CoreRetCodeEnum.RET_CODE_9999999.getCode());
        retDataHead.setMessage(CoreRetCodeEnum.RET_CODE_9999999.getMessage());
        mv.addObject("retHead", retDataHead);
        mv.addObject("retBody", ex.getMessage());
        mv.setView(view);
        ex.printStackTrace();
        return mv;
    }

    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ModelAndView notFoundErrorHandler(HttpServletRequest req, BaseException ex) {
        ModelAndView mv = new ModelAndView();
        FastJsonJsonView view = new FastJsonJsonView();
        RetDataHead retDataHead = new RetDataHead();
        retDataHead.setCode(ex.getCode());
        retDataHead.setMessage(ex.getMessage());
        mv.addObject("retHead", retDataHead);
        mv.setView(view);
        return mv;
    }

}